package com.jackrain.nea.fc.vp.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.dl.common.utils.esutil.ConvertQueryListUtil;
import com.jackrain.nea.dl.common.utils.esutil.ElasticsearchUtil;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.cp.api.ChrusersWebQueryCmd;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.fc.vp.api.ViewCvouVerifyedCmd;
import com.jackrain.nea.fc.vp.mapper.ViewCvouMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.QueryCmd;
import com.jackrain.nea.web.face.User;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.HashMap;

/**
 * POS端券核销 service端
 *
 * @author wuwenxue
 * @date 2017/11/14
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "vp")
public class ViewCvouVerifyedCmdImpl extends CommandAdapter implements ViewCvouVerifyedCmd {

    @Reference(version = "2", group = "cp")
    private QueryCmd queryCmd;

    @Reference(version = "1.0", group = "cp")
    private ChrusersWebQueryCmd chrusersWebQueryCmd;
    @Override
    @Transactional
    public ValueHolder execute(HashMap map) throws NDSException {
        ViewCvouMapper viewCvouMapper = ApplicationContextHandle.getBean(ViewCvouMapper.class);
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),
                "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCvouVerifyedCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("data")) || param.getJSONArray("data").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        User user = chrusersWebQueryCmd.getUser("INTER99999");

        JSONArray jsonArray = param.getJSONArray("data");

        if (jsonArray.size() > 100) {
            throw new NDSException("POS端数据过大，无法处理！");
        }

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());

            if (null == jsonObject.getString("ECODE")) {
                throw new NDSException("券号不能为空！");
            }

            //券号
            String ecode = jsonObject.getString("ECODE");

            int vouCount = viewCvouMapper.isVou(ecode);
            if (vouCount == 0) {
                throw new NDSException("该购物券不存在!");
            }

            /*HashMap hashMap = new HashMap(16);
            hashMap.put("tableName", "USERS");
            hashMap.put("fields", "ID,ENAME");
            hashMap.put("type", HashMap.class);
            JSONObject whereJson = new JSONObject();
            whereJson.put("NAME", "INTER99999");
            hashMap.put("whereKeys", whereJson);
            //Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.web.QueryCmd", "cp", "2");
            HashMap cpJS = (HashMap) queryCmd.queryTable(hashMap);*/

            /*//固定传输用户编号：INTER99999
            if (null == cpJS || (!cpJS.containsKey("ID")) || (!cpJS.containsKey("ENAME"))) {
                throw new NDSException("INTER99999用户错误！");
            }
            Long user = (Long) cpJS.get("ID");
            String ename = (String) cpJS.get("ENAME");*/

            //ename
            String ename = user.getEname();
            //name
            String name = user.getName();
            //用户人修改人ID
            Long userID = Integer.valueOf(user.getId()).longValue();

            JSONObject jo = new JSONObject();
            jo.put("ECODE", ecode);
            jo.put("ISVERIFYED", "Y");
            jo.put("VERIFYED_USER_ID", userID);
            jo.put("VERIFYED_TIME", timestamp);
            jo.put("MODIFIERID", userID);
            jo.put("MODIFIEDDATE", timestamp);
            jo.put("MODIFIERNAME", name);
            jo.put("MODIFIERENAME", ename);

            int update = viewCvouMapper.updateToVerifyed(jo);
            if (update < 0) {
                throw new NDSException("核销失败");
            } else {
                vh.put("status", 0);
                vh.put("message", Resources.getMessage("核销成功"));
            }
        }


        return vh;
    }
}
